package acwing;

import java.io.*;
import java.util.*;

public class ac4047 {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
    static final int N = 410, INF = 0x3f3f3f3f;
    static int n, m;
    static int[][] g = new int[N][N], f = new int[N][N];

    static int cin() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    static int floyd(int[][] d) {
        if (d[1][n] == 1) return 1;
        for (int k = 1; k <= n; k ++ ) {
            for (int i = 1; i <= n; i ++ ) {
                for (int j = 1; j <= n; j ++ ) {
                    d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }
        return d[1][n];
    }

    public static void main(String[] args) throws IOException {
        n = cin(); m = cin();
        for (int i = 0; i < N; i ++ ) {
            Arrays.fill(g[i], INF);
            Arrays.fill(f[i], INF);
        }
        while (m -- > 0) {
            int a = cin(), b = cin();
            g[a][b] = 1;
            g[b][a] = 1;
        }
        for (int i = 1; i <= n; i ++ ) {
            for (int j = 1; j <= n; j ++ ) {
                if (i != j && g[i][j] != 1) {
                    f[i][j] = 1;
                    f[j][i] = 1;
                }
            }
        }
        int res = Math.max(floyd(g), floyd(f));
        if (res == INF) out.println(-1);
        else out.println(res);
        out.close();
    }
}